package org.gdgbologna.devfest;

import org.gdgbologna.resources.Talk;

import android.content.Intent;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DetailFragment extends DialogFragment implements OnClickListener {
	
	Talk talk;
	MainActivity app;

	public static DetailFragment newInstance() {
		DetailFragment frag = new DetailFragment();
		frag.setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog);
        return frag;
    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        app = (MainActivity) getActivity();
        talk = app.app.selected;
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
		app.app.tracker.trackPageView("/talk/"+talk.getTitle());
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detail_fragment, container);
        TextView title = (TextView) view.findViewById(R.id.title_talk);
        //getDialog().setTitle("Hello");
        
        title.setText(talk.getTitle());
        
        if (talk.getColor().equalsIgnoreCase("green")) title.setTextColor(app.getResources().getColor(R.color.green));
        if (talk.getColor().equalsIgnoreCase("red")) title.setTextColor(app.getResources().getColor(R.color.red));
        if (talk.getColor().equalsIgnoreCase("blue")) title.setTextColor(app.getResources().getColor(R.color.blue));
        if (talk.getColor().equalsIgnoreCase("yellow")) title.setTextColor(app.getResources().getColor(R.color.yellow));
        
        Button btn = (Button) view.findViewById(R.id.share);
        btn.setOnClickListener(this);
        
        Button calendar = (Button) view.findViewById(R.id.calendar);
        if (talk.getStart() == null || talk.getEnd() == null) calendar.setVisibility(View.GONE);
        else calendar.setOnClickListener(this);
        
        /*LinearLayout ll = (LinearLayout) view.findViewById(R.id.title_container);
        if (talk.getColor().equalsIgnoreCase("green")) ll.setBackgroundColor(app.getResources().getColor(R.color.green));
        if (talk.getColor().equalsIgnoreCase("red")) ll.setBackgroundColor(app.getResources().getColor(R.color.red));
        if (talk.getColor().equalsIgnoreCase("blue")) ll.setBackgroundColor(app.getResources().getColor(R.color.blue));
        if (talk.getColor().equalsIgnoreCase("yellow")) ll.setBackgroundColor(app.getResources().getColor(R.color.yellow));*/

        TextView time = (TextView) view.findViewById(R.id.text_time);
        time.setText(talk.getTime().replace("\n", "-"));
        
        TextView place = (TextView) view.findViewById(R.id.text_place);
        place.setText(talk.getAula());
        
        TextView speaker = (TextView) view.findViewById(R.id.text_speaker);
        speaker.setText(talk.getSpeaker());
        
        TextView abstra = (TextView) view.findViewById(R.id.text_abstract);
        abstra.setText(talk.getAbstra());

        return view;
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int tag = Integer.parseInt((String)v.getTag());
		if (tag == 0){
			// share
			Intent intent = new Intent(Intent.ACTION_SEND);
		    intent.setType("text/plain");
		    intent.putExtra(Intent.EXTRA_TEXT, talk.getTitle()+" - "+talk.getSpeaker());
		    startActivity(Intent.createChooser(intent, app.getResources().getString(R.string.title_sharing_dialog)));
		}else{
			// calendar
			//Intent intent = new Intent(Intent.ACTION_INSERT)
			try{
				//Intent intent = new Intent(Intent.ACTION_EDIT)
				Intent intent = new Intent(Intent.ACTION_EDIT);
				//intent.setClassName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
                intent.setData(Events.CONTENT_URI)
		        .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, talk.getStart().getTimeInMillis())
		        .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, talk.getEnd().getTimeInMillis())
		        .putExtra(Events.TITLE, talk.getTitle())
		        .putExtra(Events.DESCRIPTION, "")
		        .putExtra(Events.EVENT_LOCATION, talk.getAula())
		        .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
				startActivity(intent);
			}catch (Exception e){
				Log.e("exception",e.getLocalizedMessage());
				Toast.makeText(v.getContext(), R.string.error_activity, Toast.LENGTH_SHORT).show();
			}
		}
	}


}
